package com.aube.interact.taste.goods;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.aube.interact.taste.IPluginBaseListener;
import com.aube.interact.taste.R;
import com.aube.interact.taste.controller.DataManager;
import com.huyn.baseframework.dynamicload.video.model.GoodsModel;
import com.huyn.baseframework.utils.SysUtil;
import com.huyn.baseframework.utils.Utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by hill on 16/11/4.
 */

public class GoodsStoreLayout extends RelativeLayout {
    private String TAG = "Aube/GoodsStoreView";
    private Context mContext;

    private IPluginBaseListener mBaseListener;

    private RelativeLayout mRoot;
    private LinearLayout mGoodsStoreItemLy, mGoodsStoreTitleLy;
    private ImageView mShopStoreClose;
    private View mBetterCloseView;

    private GoodsHotpotLayout.IAddFavorListener mAddFavorListener;
    private GoodsDetailLayout mGoodsDetailLayout;

    private int mTitleLyTop, mTitleLyHeight, mTitleLyWidth;

    public GoodsStoreLayout(Context context) {
        this(context, null);
    }

    public GoodsStoreLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GoodsStoreLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.aube_intera_layout_goodstore, this);
        mRoot = this;

        mGoodsStoreItemLy = (LinearLayout) findViewById(R.id.goodstore_itemly);
        mGoodsStoreTitleLy = (LinearLayout) findViewById(R.id.goodstore_titlely);
        mBetterCloseView = findViewById(R.id.goodstore_bettercloseview);

        mShopStoreClose = (ImageView) findViewById(R.id.goodsstore_close);
        mShopStoreClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mBetterCloseView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.Log(Utils.LogType.DEBUG, TAG, "GoodsStoreView onClick: Block");
            }
        });

        mTitleLyTop = SysUtil.getStatusBarHeight(mContext) - 10;
        Utils.Log(TAG, "initView mTitleLyTop = " + mTitleLyTop);
    }

    public void dismiss() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "alpha", 1.0f, 0.0f);
        animator.setDuration(300);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                setVisibility(GONE);
                mBaseListener.onDismissGoodsStore();
            }
        });
        animator.start();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mTitleLyWidth = mGoodsStoreTitleLy.getMeasuredWidth();
        mTitleLyHeight = mGoodsStoreTitleLy.getMeasuredHeight();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        mGoodsStoreTitleLy.layout(0, mTitleLyTop, mTitleLyWidth, mTitleLyTop + mTitleLyHeight);
    }

    public void bindFavorListener(GoodsDetailLayout layout, GoodsHotpotLayout.IAddFavorListener listener) {
        this.mAddFavorListener = listener;
        this.mGoodsDetailLayout = layout;
    }

    public void fillData(List<GoodsModel> dataList) {
        DataManager.getInstance(mContext).updateGoodsStore(dataList);

        // Note: add fill-in view for left side
        View fillin_view = new View(mContext);
        fillin_view.setVisibility(INVISIBLE);
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 15), Utils.dip2px(mContext, 20));
        mGoodsStoreItemLy.addView(fillin_view, param);

        Iterator<Map.Entry<Integer, GoodsModel>> iterator = DataManager.getInstance(mContext).getSortedGoodsIterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, GoodsModel> entry = iterator.next();
            final GoodsModel item = entry.getValue();
            final GoodsCartItemView view = new GoodsCartItemView(mContext);
            view.setupCallback(mAddFavorListener, mBaseListener);
            view.fillData(item, this);
            view.bindGoodsDetailLayout(mGoodsDetailLayout);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.rightMargin = Utils.dip2px(mContext, 25);
            mGoodsStoreItemLy.addView(view, params);
        }
    }

    public void setupCallback(IPluginBaseListener baseListener) {
        mBaseListener = baseListener;
    }

    /**
     * 渐现动画
     */
    public void startShowAnim() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "alpha", 0.0f, 1.0f);
        animator.setDuration(400);
        animator.setTarget(mRoot);
        animator.start();
    }

    /**
     * 渐隐动画
     */
    public void startHideAnim() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "alpha", 1.0f, 0.0f);
        animator.setDuration(400);
        animator.setTarget(mRoot);
        animator.start();
    }

    public interface IDataInflater {
        public void fillData(List<GoodsModel> goodsModelList);
    }

    public interface OnDismissEvent {
        public void onDismiss();

    }

}
